import { isBlank } from './validate'
/**
 * 表单必填校验
 * @param { string } message 校验提示说明文字
 * @return { object }
 */
export function ruleRequired (message = '该字段不能为空', trigger = '') {
  return {
    required: true,
    message: message,
    trigger,
    validator: (_rule, value, callback) => {
      if (isBlank(value)) {
        return callback(new Error(message))
      }
      callback()
    }
  }
}

/**
 * 最大长度限制校验
 * @param { number } max
 * @param { String } label
 * @return { object }
 */
export function ruleMaxLength (max, label = '输入字符') {
  return {
    max,
    message: `${label}不能超过${max}位`,
    trigger: ''
  }
}

/**
 * 最小长度限制校验
 * @param { number } min
 * @param { String } label
 * @return { object }
 */
export function ruleMinLength (min, label = '请输入') {
  return {
    min,
    message: `${label}至少${min}位字符`,
    trigger: ''
  }
}

/**
 * 整数校验
 */
export const ONLY_INTSGER = {
  pattern: /^[0-9]\d*$/,
  message: '必须输入整数',
  trigger: ''
}

/**
 * 手机号
 */
export const RULE_PHONE = {
  pattern: /^1[3456789]\d{9}$/,
  message: '手机号格式不正确',
  trigger: ''
}

/**
 * 邮箱
 */
export const RULE_EMAIL = {
  pattern: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
  message: '邮箱格式不正确',
  trigger: 'blur'
}

/**
 * 身份证
 */
export const RULE_IDCARD = {
  pattern: /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/,
  message: '身份证格式不正确',
  trigger: 'blur'
}

export const PASSWORD_RULE = {
  pattern: /^(?![A-z0-9]+$)(?=.[^%&',;=?$\x22])(?=.*[a-zA-Z])(?=.*[0-9]).{8,20}$/,
  message: '密码长度为8-20位，并且至少包含字母，数字和符号',
  trigger: 'blur'
}

export const RULE_INT_ENGLISH = {
  pattern: /^[a-z0-9]+$/i,
  message: '只能输入数字和英文字母',
  trigger: 'blur'
}

export const RULE_INT_ENGLISH_BOTLINE = {
  pattern: /^[a-z0-9_]+$/i,
  message: '只能输入数字,英文字母和下划线',
  trigger: 'blur'
}

export const RULE_POSITIVEINTEGERONLY_WITHZERO = {
  pattern: /^(0|[1-9]\d*)$/,
  message: '必须输入0或者两位正整数',
  trigger: 'blur'
}

export const NAME_CN = {
  pattern: /^[\u4e00-\u9fa5a-zA-Z0-9_-]+$/,
  message: '只能输入中文、英文字母、下划线、数字、短横线',
  trigger: 'blur'
}

export const RULE_NOSPACE = {
  pattern: /^[^\s]*$/,
  message: '请不要使用空格',
  trigger: 'blur'
}
